package com.taxx.controller;

import com.taxx.common.Result;
import com.taxx.dto.LoginRequest;
import com.taxx.dto.LoginResponse;
import com.taxx.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户认证控制器
 */
@RestController
@RequestMapping("/auth")
@CrossOrigin
public class AuthController {
    
    @Autowired
    private SysUserService userService;
    
    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result<LoginResponse> login(@Validated @RequestBody LoginRequest request) {
        try {
            LoginResponse response = userService.login(request);
            return Result.success("登录成功", response);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 用户登出
     */
    @PostMapping("/logout")
    public Result<Void> logout(HttpServletRequest request) {
        try {
            String token = getTokenFromRequest(request);
            if (token != null) {
                userService.logout(token);
            }
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 刷新令牌
     */
    @PostMapping("/refresh")
    public Result<String> refreshToken(HttpServletRequest request) {
        try {
            String token = getTokenFromRequest(request);
            if (token == null) {
                return Result.unauthorized("令牌不能为空");
            }
            
            String newToken = userService.refreshToken(token);
            return Result.success("令牌刷新成功", newToken);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 获取当前用户信息
     */
    @GetMapping("/info")
    public Result<LoginResponse.UserInfo> getCurrentUserInfo(HttpServletRequest request) {
        try {
            String token = getTokenFromRequest(request);
            if (token == null) {
                return Result.unauthorized("令牌不能为空");
            }
            
            // 这里应该从JWT中解析用户ID，暂时返回模拟数据
            LoginResponse.UserInfo userInfo = new LoginResponse.UserInfo();
            userInfo.setId(1L);
            userInfo.setUsername("admin");
            userInfo.setRealName("系统管理员");
            userInfo.setEmail("admin@taxx.com");
            userInfo.setPhone("13800138000");
            userInfo.setStatus(1);
            userInfo.setRoles(java.util.Arrays.asList("ADMIN"));
            
            return Result.success("获取用户信息成功", userInfo);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 从请求中获取令牌
     */
    private String getTokenFromRequest(HttpServletRequest request) {
        String bearerToken = request.getHeader("Authorization");
        if (bearerToken != null && bearerToken.startsWith("Bearer ")) {
            return bearerToken.substring(7);
        }
        return null;
    }
}

